package com.alice.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author alice
 * @date 2021/9/24
 * @apiNote
 */
@Controller
public class CsvController {

    @GetMapping ("csv")
    public void csv(HttpServletResponse httpServletResponse){
        String fileName="tree.txt";
        httpServletResponse.setHeader("content-type","text/plain");
        //httpServletResponse.setHeader("content-type","application/x-msdownload;");
        httpServletResponse.setContentType("text/plain;charset=utf-8");
        //httpServletResponse.setHeader("Content-Disposition","attachment;filename="+fileName);
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;

        try{
            os = httpServletResponse.getOutputStream();
            File file;
            bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
            int i = bis.read(buff);
            while (i!=-1){
                os.write(buff,0,buff.length);
                os.flush();
                i = bis.read(buff);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
